package org.niux.AutoLoginQQ.operate;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.niux.AutoLoginQQ.VO.QQNumVO;
import org.niux.AutoLoginQQ.common.Constant;
import org.niux.AutoLoginQQ.ui.Main;

/**
 * 所有任务接口 其他任务必须继承访类
 * 
 * @author obullxl
 */
public class LogoutTask implements Runnable {
	private Log log = LogFactory.getLog(this.getClass());

	/* 产生时间 */
	private Date generateTime = null;
	/* 提交执行时间 */
	private Date submitTime = null;
	/* 开始执行时间 */
	private Date beginExceuteTime = null;
	/* 执行完成时间 */
	private Date finishTime = null;

	private long taskId;

	private Main gui;

	private QQNumVO qqNumVO;
	
	private String status;

	public LogoutTask(QQNumVO qqNumVO, Main gui,String status) {
		this.generateTime = new Date();
		this.qqNumVO = qqNumVO;
		this.gui = gui;
		this.status = status;
	}

	/**
	 * 任务执行入口
	 */
	public void run() {

		HttpClient httpclient = qqNumVO.getHttpClient();

		String psessionid = qqNumVO.getPsessionid();

		HttpGet httpget = new HttpGet(
				"http://d.web2.qq.com/channel/logout2?ids=&"
						+ "clientid=50705043&psessionid=" + psessionid + "&t="
						+ getUTCTime());

		// 设置http头信息
		httpget.setHeader("Accept", "*/*");
		httpget.setHeader("Accept-Encoding", "gzip, deflate");
		httpget.setHeader("Accept-Language", "zh-cn");
		httpget.setHeader("Connection", "Keep-Alive");
		httpget.setHeader("Content-Type", "utf-8");
		httpget.setHeader("Host", "d.web2.qq.com");
		httpget.setHeader("Referer",
				"http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=2");
		httpget.setHeader("User-Agent",
				"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

		String responseString = "";

		LoginQQ login = new LoginQQ(gui);
		
		try {
			HttpResponse response = httpclient.execute(httpget);

			// 获取响应实
			HttpEntity entity = response.getEntity();

			responseString = EntityUtils.toString(entity);

			log.debug(responseString);

			Constant.QQ_LIST.get(qqNumVO.getQqNum()).setLogin(false);

			Constant.QQ_LIST.get(qqNumVO.getQqNum()).setAlreadyLogin(System.currentTimeMillis() - qqNumVO.getLoginTime() + 
					(Constant.QQ_LIST.get(qqNumVO.getQqNum()).getAlreadyLogin() == null ? 0 : Constant.QQ_LIST.get(qqNumVO.getQqNum()).getAlreadyLogin()));

//			Constant.QQ_LIST.remove(qqNumVO.getQqNum());
//
//			Constant.QQ_LIST.put(qqNumVO.getQqNum(), qqNumVO);

			login.updateStatus(qqNumVO.getIndex(),qqNumVO.getQqNum(), status);// 更新界面表格
			
			login.updateOptStatus(qqNumVO.getIndex(),qqNumVO.getQqNum(), "退出成功"); // 更新界面表格

			// 把退出的QQ移出登录队列中
			Iterator<QQNumVO> i = Constant.ALREADY_LOGIN_QUEEN.iterator();

			while (i.hasNext()) {
				if (i.next().getQqNum().equals(qqNumVO.getQqNum())) {
					i.remove();
				}
			}

			Constant.LOGIN_COUNT = Constant.ALREADY_LOGIN_QUEEN.size();
			
			login.updateAlreadyLogin(Integer.toString(Constant.ALREADY_LOGIN_QUEEN
					.size()));

		} catch (IOException e) {
			log.error("网络连接超时", e);
			login.updateOptStatus(qqNumVO.getIndex(),qqNumVO.getQqNum(), "网络连接超时");
			e.printStackTrace();
		}
		this.finishTime = new Date();
	}

	/**
	 * 
	 * 方法概述：获取时间戳 
	 * 描述：获取时间戳
	 * 
	 * @return 
	 * 返回类型：long 
	 * 修改人：梁铭显 
	 * 修改时间：2012-9-10
	 */
	public long getUTCTime() {
		return System.currentTimeMillis();
	}
	
	public Date getGenerateTime() {
		return generateTime;
	}

	public Date getBeginExceuteTime() {
		return beginExceuteTime;
	}

	public void setBeginExceuteTime(Date beginExceuteTime) {
		this.beginExceuteTime = beginExceuteTime;
	}

	public Date getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}

	public Date getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(Date submitTime) {
		this.submitTime = submitTime;
	}

	public long getTaskId() {
		return taskId;
	}

	public void setTaskId(long taskId) {
		this.taskId = taskId;
	}

}
